<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Goods as GoodsModel;
use app\common\model\Store;
use Exception;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Unionpay;
use app\common\controller\Alipay;
use function foo\func;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class My extends Home
{
    public $noNeedLogin = [ ];


   
    /**
     * 修改用户头像
     */
    public function edit_avatar()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = $this->request->post();
            if (empty($data['avatar'])) {
                $this->result('请上传头像', '', 204);
            }
            $re = Db::name('qt_users')->where('id', $userInfo['id'])->update(['avatar' => $data['avatar']]);
            if ($re !== false) {
                $this->result('修改成功', '', 200);
            } else {
                $this->result('修改失败', '', 204);
            }
        } else {
            $this->result('非法请求', '', 204);
        }
    }


    /**
     * 关于我们
     */
    public function about(){
        $data = Db::name('qt_company_config')->find();
        $data['logo'] = get_img_path($data['logo']);
        $this->result('请求成功',$data,200);
    }


    /**
     * 获取上下级部门
     */
    public function get_group(){
        if($this->request->isGet()){
            $gateData = $this->request->get();
            if(empty($gateData['type'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = $this->findGroup($userInfo['group_id'],$gateData['type']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 获取下级部门
     */
    public function down_group(){
        if($this->request->isGet()){
            $userInfo = $this->userInfo;
            if($userInfo['rank'] == '3'){
                $data = Db::name('qt_group')->field('id as group_id,name')->where('team_id',$userInfo['team_id'])->select();
                $this->result('查询成功',$data,200);
            }
            $data = $this->group($userInfo['group_id']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


   public function getTree($list,$pid=0,$level=0) {
      static $tree = array();
      foreach($list as $row) {
             if($row['pid']==$pid) {
                    $row['level'] = $level;
              $tree[] = $row;
              $this->getTree($list, $row['id'], $level + 1);
          }
     }
     return $tree;
 }


    private function getGroup($groupId){
        static $res = array();
        $groupRes = Db::name('qt_group')->where('pid',$groupId)->select();
        if(empty($groupRes)){
            return $res;
        }else{
            foreach ($groupRes as $key=>$value){
                $res[] = $value;
                $chileGroupRes = Db::name('qt_group')->where('pid',$value['id'])->select();
                if(!empty($chileGroupRes)){
                    $this->getGroup($value['id']);
                }
            }
            return $res;
        }
    }


    /**
     * 获取上下级岗位
     */
    public function get_post(){
        if($this->request->isGet()){
            $gateData = $this->request->get();
            if(empty($gateData['type'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = $this->findPost($userInfo['post_id'],$gateData['type']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }


   


    /**
     * 查找日报部门
     * @param $userId [当前用户ID]
     * @param $type [类型 1 向上查找 2 向下查找]
     */
    public function group($groupId){
        //向上查找
        $groupRes = Db::name('qt_group')->where('status','1')->field('id,name,pid')->select();
        $data  = $this->get_downline($groupRes,$groupId);

        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['group_id'] = $value['id'];
            $result[$key]['name'] = $value['name'];
        }
        return $result;


    }



    //获取用户的所有下级ID
    function get_downline($members,$mid,$level=0){
        $arr=array();
        foreach ($members as $key => $v) {
            if($v['pid']==$mid){  //pid为0的是顶级分类
                $v['level'] = $level+1;
                $arr[]=$v;
                $arr = array_merge($arr,$this->get_downline($members,$v['id'],$level+1));
            }
        }
        return $arr;
    }

}
